package middleware

import (
	"XSSS_v2/handler"
	"XSSS_v2/pkg/casbin"
	"XSSS_v2/pkg/errno"
	"github.com/gin-gonic/gin"
)

func PermissionMiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {

		user := c.MustGet("user").(string)
		obj := c.MustGet("obj").(string)

		// 检查用户权限
		isPass, err := casbin.Enforcer.Enforce(user, obj, c.Request.Method)
		if err != nil {
			handler.SendResponse(c, err, nil)
			c.Abort()
			return
		}
		if isPass {
			c.Next()
		} else {
			handler.SendResponse(c, errno.ErrStudentPermission, nil)
			c.Abort()
			return
		}
	}
}
